<?php
/*
 * Nippy
 * @author Charly Poly <charly[dot]poly at live[dot]fr>
 */

class Nippy_Bench {

    private $mark = array();

    public function mark($mark) {
        $this->mark[$mark] = array();
        $this->mark[$mark]['time'] = microtime();
        $this->mark[$mark]['memory_u'] = memory_get_usage();
        $this->mark[$mark]['memory_p'] = memory_get_peak_usage();
    }
    
    public function end($mark) {
        $this->mark[$mark.'_end'] = array();
        $this->mark[$mark.'_end']['time'] = microtime();
        $this->mark[$mark.'_end']['memory_u'] = memory_get_usage();
        $this->mark[$mark.'_end']['memory_p'] = memory_get_peak_usage();
    }


    public function compare($a, $b) { // a < b !
        $timeElapsed = round( ($this->mark[$b]['time'] - $this->mark[$a]['time']) , 5);
        return array('time'=>$timeElapsed); // , 'memory'=>$memory);
    }

    public function get($mark) {
        if (isset($this->mark[$mark.'_end'])) {
            return $this->compare($this->mark[$mark], $this->mark[$mark.'_end']);
        } else {
            throw new Nippy_SystemException(__CLASS__, 'Call to an without-ending mark : "'.$mark.'"');
        }
    }

}
?>
